package com.cookie.meetproject.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cookie.framework.base.BaseFragment;
import com.cookie.framework.bmob.BmobManger;
import com.cookie.framework.bmob.IMUser;
import com.cookie.framework.event.EventManager;
import com.cookie.framework.event.MessageEvent;
import com.cookie.framework.helper.GlideHelper;
import com.cookie.meetproject.R;
import com.cookie.meetproject.ui.MeInfoActivity;
import com.cookie.meetproject.ui.NewFriendActivity;
import com.cookie.meetproject.ui.PrivateSetActivity;
import com.cookie.meetproject.ui.ShareImageActivity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.meetproject.fragment
 * 文件名:    MeFragment
 * 创建者:    cookie
 * 创建时间:   2020/1/5 0:42
 * 描述:      我的
 */

public class MeFragment extends BaseFragment implements View.OnClickListener {

    private CircleImageView ivMePhoto;
    private TextView tvNickname;
    private TextView tvServerStatus;
    private LinearLayout llMeInfo;
    private LinearLayout llNewFriend;
    private LinearLayout llPrivateSet;
    private LinearLayout llShare;
    private LinearLayout llNotice;
    private LinearLayout llSetting;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        ivMePhoto = view.findViewById(R.id.iv_me_photo);
        tvNickname = view.findViewById(R.id.tv_nickname);

        llMeInfo = view.findViewById(R.id.ll_me_info);
        llNewFriend = view.findViewById(R.id.ll_new_friend);
        llPrivateSet = view.findViewById(R.id.ll_private_set);
        llShare = view.findViewById(R.id.ll_share);
        llSetting = view.findViewById(R.id.ll_setting);
        llNotice = view.findViewById(R.id.ll_notice);
        tvServerStatus = view.findViewById(R.id.tv_server_status);

        llMeInfo.setOnClickListener(this);
        llNewFriend.setOnClickListener(this);
        llPrivateSet.setOnClickListener(this);
        llShare.setOnClickListener(this);
        llSetting.setOnClickListener(this);
        llNotice.setOnClickListener(this);

        loadMeInfo();
    }

    //加载个人信息
    private void loadMeInfo() {
        IMUser user = BmobManger.getInstance().getUser();
        GlideHelper.loadSmollUrl(getActivity(), user.getPhoto(), 100, 100, ivMePhoto);
        tvNickname.setText(user.getNickName());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_new_friend:
                startActivity(new Intent(getActivity(), NewFriendActivity.class));
                break;
            case R.id.ll_share:
                startActivity(new Intent(getActivity(), ShareImageActivity.class));
                break;
            case R.id.ll_private_set:
                startActivity(new Intent(getActivity(), PrivateSetActivity.class));
                break;
            case R.id.ll_me_info:
                startActivity(new Intent(getActivity(), MeInfoActivity.class));
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        switch (event.getType()) {
            case EventManager.EVENT_REFRE_ME_INFO:
                loadMeInfo();
                break;
        }
    }
}
